[ORG 0x7c00]
BOOT_MAIN_ADDR equ 0x500
[SECTION .text]
[BITS 16]
global boot_start
boot_start:
    mov ax, 3
    int 0x10

    mov edi, BOOT_MAIN_ADDR
    mov ecx, 1
    mov bl, 2
    call read_hd

    mov si, jmp_to_setup
    call print

    jmp BOOT_MAIN_ADDR

read_hd:
    mov dx, 0x1f2
    mov al, bl
    out dx, al

    mov dx, 0x1f3
    mov al, cl
    out dx, al

    mov dx, 0x1f4
    mov al, ch
    out dx, al

    shr ecx, 16
    mov dx, 0x1f5
    mov al, cl
    out dx, al

    mov dx, 0x1f6
    mov al, ch
    and al, 0x0f
    or al, 0b1110_0000
    out dx, al

    mov dx, 0x1f7
    mov al, 0x20
    out dx, al

    xor cx, cx
    mov cl, bl
.start_read:
    push cx
    call wait_hd
    call read_hd_data
    pop cx
    loop .start_read
    ret

wait_hd:
    mov dx, 0x1f7
.check:
    in al, dx
    and al, 0x88
    cmp al, 0x08
    jnz .check
    ret

read_hd_data:
    mov cx, 256
    mov dx, 0x1f0
.read_word:
    in ax, dx
    mov [edi], ax
    add edi, 2
    loop .read_word
    ret

jmp_to_setup:
    db "jmp to setup", 10, 13, 0

print:
    mov ah, 0xe
    mov bh, 0
    mov bl, 1
.repeat:
    mov al, [si]
    test al, al
    jz .done
    int 0x10
    inc si
    jmp .repeat
.done:
    ret

times 510 - ($ - $$) db 0
dw 0xaa55